﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using GameBase.Form.Control;

namespace GameBase.Form
{
    public class MyForm : VisibleGroup
    {
        public static float MIN_DEPTH;
        Cursor cursor;
        private Vector2 screenScale;

        public Vector2 ScreenScale
        {
            get { return screenScale; }
            set { screenScale = value; }
        }
        //private List<MyControl> controls;
        private int width;
        private int height;

        MyControl focusedControl;

        public MyControl FocusedControl
        {
            get { return focusedControl; }
            set { focusedControl = value; }
        }
        Keys[] lastKeyPressed = new Keys[0];
        public int Width
        {
            get { return width; }
        }

        public int Height
        {
            get { return height; }
        }
        public override Vector2 BasePosition
        {
            get
            {
                return base.BasePosition;
            }
            set
            {
                basePosition = Vector2.Zero;
            }
        }
        public void Add(MyControl control)
        {
            control.ScreenScale = ScreenScale;
            control.BasePosition = position * ScreenScale;
            listChildren.Add(control);
            control.Depth = 0.01f;
        }
   
        public MyForm(int formWidth,int formHeight,int screenWidth, int screenHeight)
        {
            screenScale = new Vector2((float)screenWidth / formWidth,
                (float)screenHeight/formHeight);
            cursor = new Cursor();
            cursor.Color = MyControl.GetAColor();
            height = formHeight;
            width = formWidth;
           
        }
        public void SetScreen(int formWidth, int formHeight, int screenWidth, int screenHeight)
        {
            ScreenScale = new Vector2((float)screenWidth / formWidth,
                (float)screenHeight / formHeight);
            height = formHeight;
            width = formWidth;
            foreach (MyControl control in listChildren)
            {
                 control.ScreenScale = ScreenScale;
            }
        }
        public bool MouseInput(MouseState mouseState)
        {
            bool flag = false;
            foreach (MyControl control in listChildren)
            {
                if (mouseState.LeftButton == ButtonState.Pressed)
                {
                    cursor.Press();
                }
                else
                {
                    cursor.Release();
                }
                MyControl ctrl = control.MouseInput(mouseState);
                if (ctrl != null)
                {
                    if (ctrl.Visible)
                    {
                        flag = true;
                    }
                    if (FocusedControl != null)
                    {
                        FocusedControl.IsForcused = false;
                    }
                    FocusedControl = ctrl;
                    FocusedControl.IsForcused = true;
                }
            }
            return flag;
        }
        public override void Update(GameTime gameTime)
        {
            
            MIN_DEPTH = 1;
            cursor.Update(gameTime);
            MouseState mouseState = Mouse.GetState();
            bool mousePressed = mouseState.LeftButton == ButtonState.Pressed;
            
            List<Keys> releasedKeys = new List<Keys>(lastKeyPressed);
            lastKeyPressed = Keyboard.GetState().GetPressedKeys();
            List<Keys> pressedKeys = new List<Keys>(lastKeyPressed);

            for (int i = 0; i < pressedKeys.Count;i++ )
            {
                for (int j = 0; j < releasedKeys.Count;j++ )
                {
                    if (pressedKeys[i] == releasedKeys[j])
                    {
                        pressedKeys.RemoveAt(i);
                        releasedKeys.RemoveAt(j);
                        i--;
                        j--;
                        break;
                    }
                }
            }
            foreach (MyControl control in listChildren)
            {
                control.Update(gameTime);
            }
            if (FocusedControl != null)
            {
                FocusedControl.KeyPressed(pressedKeys);
                FocusedControl.KeyRelease(releasedKeys);
            }
        }
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            foreach (MyControl control in listChildren)
            {
                control.Draw(gameTime, spriteBatch);
            }
            cursor.Draw(gameTime, spriteBatch);
        }
        
    }
}
